package com.borened.common.core.mybatisplus;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.borened.common.core.base.BasePageQuery;
import com.borened.common.core.base.result.PageResult;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Map;

/**
 * * 在mybatisplus 的ServiceImpl的基础上， 对业务共性方法，再封装。
 *
 * @author: cch
 * @since 2021/12/4 15:46
 */
@Deprecated
public interface IBaseService<T> extends IService<T> {

    /**
     * 请使用BaseController替换
     *
     * @param query
     * @return
     * @see BaseController
     * 按query条件 等值查询的分页实现  (不支持关联对象查询，如有复杂查询请自定义mapper方法)
     * { @link https://baomidou.com/guide/interceptor-pagination.html#paginationinnerinterceptor }
     * @see com.borened.system.service.impl.SysUserServiceImpl#listByCondition
     */
    @Deprecated
    default PageResult<T> pageEqQuery(BasePageQuery query) {
        IPage<T> page = new Page<>(query.getPageNum(), query.getPageSize());
        QueryWrapper<T> wrapper = new QueryWrapper<>();

        Map<String, Object> params = BeanUtil.beanToMap(query, true, true);

        Field[] fields = BasePageQuery.class.getDeclaredFields();

        //只需要实体本身的属性条件。
        wrapper.allEq((key, value) -> {
            boolean match = Arrays.stream(fields).anyMatch(field -> StrUtil.toUnderlineCase(field.getName()).equals(key));
            return !match;
        }, params, false);

        IPage<T> pageResult = this.getBaseMapper().selectPage(page, wrapper);
        return new PageResult<>(pageResult);
    }
}
